package br.com.clebao.droidcontroler;

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import br.com.clebao.droidcontroler.modelo.Cidade;
import br.com.clebao.droidcontroler.modelo.Filial;
import br.com.clebao.droidcontroler.persistencia.DAOFilial;

public class FilialPrincipal extends ListActivity {
	
	private DAOFilial dao = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filialmain);
		dao = new DAOFilial(this);
		registerForContextMenu(getListView());
		
		Filldata();
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater mi = getMenuInflater();
		mi.inflate(R.menu.menuoption_filial, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	


	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, R.integer.EDITAR, 0, R.string.menu_editar);
		menu.add(0,R.integer.EXCLUIR, 1, R.string.menu_excluir);
	}


	private void Filldata() {
		Cursor c = dao.findAllObject();
		startManagingCursor(c);
		
		String [] from = new String[]{"_id", "nomefantasia"};
		int[] to = new int[]{R.id.textView1, R.id.textView2};
		
		SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.listview_row, c, from, to);
		
		setListAdapter(sca);
	}


	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		Cursor c = dao.findAllObject();
		startManagingCursor(c);
		
		c.moveToPosition(position);
		
		Resources res = getResources();
		
		Intent in = new Intent(this, FilialEdit.class);
		in.putExtra(res.getString(R.string.ID), c.getInt(c.getColumnIndexOrThrow("_id")));
		in.putExtra(res.getString(R.string.NOMEFANTASIA), c.getString(c.getColumnIndexOrThrow("nomefantasia")));
		in.putExtra(res.getString(R.string.NOMERAZAO), c.getString(c.getColumnIndexOrThrow("nomerazao")));
		in.putExtra(res.getString(R.string.CNPJ), c.getString(c.getColumnIndexOrThrow("cnpj")));
		in.putExtra(res.getString(R.string.INSC_EST), c.getString(c.getColumnIndexOrThrow("inscest")));
		in.putExtra(res.getString(R.string.ENDERECO), c.getString(c.getColumnIndexOrThrow("endereco")));
		in.putExtra(res.getString(R.string.NUMERO), c.getString(c.getColumnIndexOrThrow("numero")));
		in.putExtra(res.getString(R.string.COMPLEMENTO), c.getString(c.getColumnIndexOrThrow("complemento")));
		in.putExtra(res.getString(R.string.BAIRRO), c.getString(c.getColumnIndexOrThrow("bairro")));
		in.putExtra(res.getString(R.string.CEP), c.getString(c.getColumnIndexOrThrow("cep")));
		in.putExtra(res.getString(R.string.CIDADE), c.getInt(c.getColumnIndexOrThrow("id_cidade")));
		
		startActivityForResult(in, R.integer.EDITAR);
		
		super.onListItemClick(l, v, position, id);
	}


	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		
		Intent in = new  Intent(this, FilialEdit.class);
		startActivityForResult(in, R.integer.INCLUIR);
		
		return super.onMenuItemSelected(featureId, item);
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (data == null){
			return; 
		}
		
		switch (requestCode){
		case R.integer.INCLUIR:
			dao.addObject(desempacatarDados(data.getExtras(), requestCode));
			break;
			
		case R.integer.EDITAR:
			dao.updateObject(desempacatarDados(data.getExtras(), requestCode));
			break;			
		}
		
	}
	
	
	private Filial desempacatarDados(Bundle extras, int codResult){
		Resources res = getResources();
		Filial filial = new Filial();
		
		if (codResult == R.integer.EDITAR){
			filial.setId(extras.getInt(res.getString(R.string.ID)));
		}
		
		filial.setFantasia(extras.getString(res.getString(R.string.NOMEFANTASIA)));
		filial.setRazao(extras.getString(res.getString(R.string.NOMERAZAO)));
		filial.setCnpj(extras.getString(res.getString(R.string.CNPJ)));
		filial.setInscEstadual(extras.getString(res.getString(R.string.INSC_EST)));
		filial.setEndereco(extras.getString(res.getString(R.string.ENDERECO)));
		filial.setNumero(extras.getString(res.getString(R.string.NUMERO)));
		filial.setComplemento(extras.getString(res.getString(R.string.COMPLEMENTO)));
		filial.setBairro(extras.getString(res.getString(R.string.BAIRRO)));
		filial.setCep(extras.getString(res.getString(R.string.CEP)));
		filial.setCidade(new Cidade(extras.getInt(res.getString(R.string.CIDADE)), null));	
		
		return filial;		
	}
	
	
	
}
